package com.angel.sensors.ui;

import java.util.HashMap;

import com.angel.sensors.model.BaseMeter;
import com.angel.sensors.model.NumericComponent;
import com.angel.sensors.model.SensorComponent;
import com.angel.sensors.model.StringComponent;

import com.angel.sensors.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * Vista que muestra los valores de los distintos sensores
 * 
 */

public class SensorView extends LinearLayout {    
    private TextView titleView;
    private HashMap<String, SensorComponentView> componentViews = new HashMap<String, SensorComponentView>();
            
    public SensorView(Context context, BaseMeter meter) {
        super(context);
        
        setOrientation(VERTICAL);
        
        titleView = new TextView(context);
        titleView.setText(meter.getText());
        titleView.setTextAppearance(context, android.R.style.TextAppearance_Large);
        titleView.setTextColor(getResources().getColor(R.color.title_text));
        
        addView(titleView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        
        for (SensorComponent c: meter.getComponents()) {
            SensorComponentView view = new SensorComponentView(context, c);
            componentViews.put(c.getName(), view);
            addView(view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        }
    }
    
    public SensorView(Context context) {
    	super(context);
    }
    
    public void setTitle(String title) {
        titleView.setText(title);
    }
    
    public void update() {        
        for (SensorComponentView view: componentViews.values()) {
            view.update();
        }
    }

    private class SensorComponentView extends LinearLayout {
        private static final int VALUE_RANGE = 10000;
        
        private TextView textView;
        private ProgressBar valueBar;
        private SensorComponent component;
        
        SensorComponentView(Context context, SensorComponent comp) {
            super(context);
            
            component = comp;
            
            setOrientation(VERTICAL);            
            
            // Creamos la vista para los valores de los sensores
            textView = new TextView(context);
            textView.setText(comp.getText() + ": -");
            addView(textView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            
            // Creamos la barra de valores
            if ((component instanceof NumericComponent) && ((NumericComponent)component).hasRange()) {                
               
                LayoutInflater inflater = (LayoutInflater)context.getSystemService
                        (Context.LAYOUT_INFLATER_SERVICE);
                View view = (View)inflater.inflate(R.layout.sensor_value_bar, null);
                
                valueBar = (ProgressBar)view.findViewById(R.id.progressBar);
                valueBar.setMax(VALUE_RANGE);
                valueBar.setMinimumHeight(20);
                
                addView(view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            }
        }
        
        void update() {
            if (component instanceof StringComponent) {
                textView.setText(component.getText() + ": " + component.getValue());
            } else if (component instanceof NumericComponent) {
                NumericComponent numeric = (NumericComponent)component;
                textView.setText(component.getText() + ": " + numeric.getValueWithUnit());
                if (numeric.hasRange()) {
                    int progress = (int)(VALUE_RANGE * (numeric.getCurrent() - numeric.getMinValue()) / 
                            (numeric.getMaxValue() - numeric.getMinValue())
                        );
                    valueBar.setProgress(progress);
                }
            }
        }
    }
}
